package cn.com.zsw.gblog.modules.common.contrller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.regex.Pattern;


/**
 * @author: shiwangZhou
 * @description:
 * @date: 2020-10-09 19:48
 */

@Slf4j
public class BaseController {
    protected final Pattern Phone_PT = Pattern.compile("^1[35789]\\d{9}$");
    private final Pattern PT_DATE = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");

    public HttpServletRequest getCurrentRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            throw new IllegalStateException("当前线程中不存在 Request 上下文");
        }
        return attrs.getRequest();
    }

    public HttpServletResponse getCureentResponse() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            throw new IllegalStateException("当前线程中不存在 Response 上下文");
        }
        return attrs.getResponse();
    }

    public HttpSession getCurrentSession() {
        return getCurrentRequest().getSession();
    }

}
